home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Applications / P4⁄Mac 2.0d4 / Mac source 2.0 / EditWindow.p < prev    next >
Encoding:
Text File  |  1996-09-28  |  10.5 KB  |  469 lines  |  [TEXT/PJMM]

  1. {P4/Mac port by Ingemar Ragnemalm 1994-1996}
  2.  
  3. unit EditWindow;
  4.  
  5. interface
  6.  
  7.     uses
  8.         TransEdit, TransSkel;
  9.  
  10.  
  11.     procedure InitEditWindow;
  12.  
  13.  
  14. implementation
  15.  
  16.     const
  17.         maxSize = 8;        { no. font sizes made available }
  18.         hSize = 300;        { horiz, vert size of new windows }
  19.         vSize = 205;
  20.  
  21.         { File menu item numbers }
  22.  
  23.         new = 1;                { begin new window }
  24.         open = 2;                { open existing file }
  25.         close = 3;                { close file }
  26.         save = 5;                { save file }
  27.         saveas = 6;            { save under another name }
  28.         saveCopy = 7;        { save a copy w/o switching file binding }
  29.         revert = 8;            { revert to version on disk }
  30.         quit = 10;
  31.  
  32.             { Edit menu item numbers }
  33.  
  34.         undo = 1;
  35.         cut = 3;
  36.         copy = 4;
  37.         paste = 5;
  38.         clear = 6;
  39.  
  40.         { Format menu item numbers }
  41.  
  42.         wordWrap = 1;
  43.         noWrap = 2;
  44.         leftJust = 4;
  45.         centerJust = 5;
  46.         rightJust = 6;
  47.  
  48.     var
  49.         lastFront: WindowPtr;    { keeps track of front window }
  50.         fileMenu, editMenu, fontMenu, sizeMenu, formatMenu: MenuHandle;
  51.  
  52.         sizes: array[0..maxSize] of integer;
  53.  
  54.         dummy: Boolean;
  55.  
  56.         windCount: integer;    {Used for window offsets}
  57.  
  58. {    Uncheck all the items in a menu}
  59.  
  60.  
  61.     procedure UncheckMenu (theMenu: MenuHandle);
  62.         var
  63.             i, nItems: integer;
  64.  
  65.     begin
  66.         nItems := CountMItems(theMenu);
  67.  
  68.         for i := 1 to nItems do
  69.             begin
  70.                 CheckItem(theMenu, i, false);
  71.                 SetItemStyle(theMenu, i, []);
  72.             end;
  73.     end;
  74.  
  75. {    Set the Font, Size and Format menus so that the items corresponding}
  76. {    to the text characteristics of the window are checked.  If the}
  77. {    window isn't an edit window, dim all three menus.}
  78.  
  79.  
  80.     procedure SetTextMenus (drawBar: Boolean);
  81.  
  82.         var
  83.             theWind: WindowPtr;
  84.             wFontName, mFontName: str255;
  85.             i, nItems: integer;
  86.             te: TEHandle;
  87.  
  88.     begin
  89.         theWind := Frontwindow;
  90.         UncheckMenu(fontMenu);                { toss current check marks }
  91.         UncheckMenu(sizeMenu);
  92.         UncheckMenu(formatMenu);
  93.  
  94.         if not IsEWindow(theWind) then            { disable the menus }
  95.             begin
  96.                 DisableItem(fontMenu, 0);
  97.                 DisableItem(sizeMenu, 0);
  98.                 DisableItem(formatMenu, 0);
  99.             end
  100.         else
  101.             begin
  102.                 EnableItem(fontMenu, 0);
  103.                 EnableItem(sizeMenu, 0);
  104.                 EnableItem(formatMenu, 0);
  105.  
  106.                 te := GetEWindowTE(theWind);
  107.                 if te^^.crOnly < 0 then
  108.  
  109. {    Check appropriate word wrap item}
  110.  
  111.                     CheckItem(formatMenu, noWrap, true)
  112.                 else
  113.                     CheckItem(formatMenu, wordWrap, true);
  114.  
  115. {    Check appropriate justification item{}
  116.  
  117.                 case te^^.just of
  118.                     teJustLeft: 
  119.                         CheckItem(formatMenu, leftJust, true);
  120.                     teJustRight: 
  121.                         CheckItem(formatMenu, rightJust, true);
  122.                     teJustCenter: 
  123.                         CheckItem(formatMenu, centerjust, true);
  124.                     otherwise
  125.                 end;
  126.  
  127. {    Check appropriate font name item}
  128.  
  129.                 for i := 0 to maxSize - 1 do
  130.                     begin
  131.                         if te^^.txSize = sizes[i] then
  132.                             checkitem(sizeMenu, i + 1, true);
  133.                         if RealFont(te^^.txFont, sizes[i]) then
  134.                             SetItemStyle(sizeMenu, i + 1, [outline])
  135.                         else
  136.                             SetItemStyle(sizeMenu, i + 1, []);
  137.                         GetFontName(te^^.txFont, wFontName);
  138.                         nItems := CountMItems(fontMenu);
  139.                     end;
  140.                 for i := 1 to nItems - 1 do
  141.                     begin
  142.                         GetMenuItemText(fontmenu, i, mFontName);
  143.                         if EqualString(wFontName, mFontName, false, true) then
  144.                             CheckItem(fontMenu, i, true);
  145.                     end;
  146.                 if drawBar then
  147.                     DrawMenuBar;
  148.             end;
  149.     end;
  150.  
  151. {    Set File/Edit menu items according to type of front window.}
  152.  
  153. {    The general behavior is:}
  154.  
  155. {    New and Open always enabled, since a new edit window can always be}
  156. {    opened.}
  157.  
  158. {    Close enabled when an edit or DA window in front (i.e., when there's}
  159. {    a window at all).}
  160.  
  161. {    Save enabled for edit windows not bound to a file, and edit windows}
  162. {    bound to a file when they're dirty (typed into, Edit menu used to}
  163. {    do something to them).}
  164.  
  165. {    Save As and Save a Copy As enabled for edit windows.}
  166.  
  167. {    Revert enabled for edit windows bound to a file when they're dirty.}
  168.  
  169. {    Undo disabled when there's an edit window in front.}
  170.  
  171.     procedure SetNonTextmenus;
  172.  
  173.         var
  174.             theWind: WindowPtr;
  175.             theKind: integer;
  176.             thePeek: windowPeek;
  177.  
  178.     begin
  179.         DisableItem(fileMenu, close);    { assume no window at all }
  180.         DisableItem(fileMenu, save);
  181.         DisableItem(fileMenu, saveas);
  182.         DisableItem(fileMenu, savecopy);
  183.         DisableItem(fileMenu, revert);
  184.         EnableItem(editMenu, undo);
  185.  
  186.         theKind := 0;
  187.         theWind := FrontWindow;
  188.         thePeek := WindowPeek(theWind);
  189.         if theWind <> nil then
  190.             theKind := thePeek^.windowKind;
  191.         if theKind < 0 then                            { DA in front }
  192.             EnableItem(fileMenu, close)
  193.         else if IsEWindow(theWind) then            { edit window in front }
  194.             begin
  195.                 EnableItem(fileMenu, close);
  196.                 EnableItem(fileMenu, saveas);
  197.                 EnableItem(fileMenu, savecopy);
  198.                 if (GetEWindowFile(theWind, nil) = false) then    { not bound to file }
  199.                     EnableItem(fileMenu, save)
  200.                 else if IsEWindowDirty(theWind) then            { bound - is it dirty? }
  201.                     begin
  202.                         EnableItem(fileMenu, save);
  203.                         EnableItem(fileMenu, revert);
  204.                     end;
  205.                 DisableItem(editMenu, undo);
  206.             end;
  207.     end;
  208.  
  209. {    Background procedure.  Check front window, reset menus if it}
  210. {    changes.  The menu bar doesn't need redrawing by SetTextMenus}
  211. {    if the previous and current front window are either both edit}
  212. {    windows or both not edit windows.  This check eliminates some}
  213. {    needless menu flashing.}
  214.  
  215.     procedure CheckFront;
  216.  
  217.     begin
  218.         if FrontWindow <> lastFront then
  219.             begin
  220.                 SetNonTextMenus;
  221.                 if IsEWindow(FrontWindow) = IsEwindow(lastFront) then
  222.                     SetTextmenus(false)
  223.                 else
  224.                     SetTextmenus(true);
  225.                 lastFront := FrontWindow;
  226.             end;
  227.     end;
  228.  
  229. {    Got an activate or deactivate.  It doesn't matter which, really.}
  230. {    Set the text menus appropriately for the front window, and draw}
  231. {    the menu bar, as these menus might change state from enabled to}
  232. {    disabled or vice-versa.}
  233.  
  234.  
  235.     procedure Activate (active: Boolean);
  236.     begin
  237.         CheckFront;
  238.     end;
  239.  
  240. {    Got a keyclick in an edit window.}
  241.  
  242.  
  243.     procedure Key;
  244.     begin
  245.         SetNonTextMenus;
  246.     end;
  247.  
  248. {    Close selected from File menu, or close box of edit window}
  249. {    clicked.}
  250.  
  251.     procedure myClose;
  252.         var
  253.             theWind: WindowPtr;
  254.             ignore: Boolean;
  255.  
  256.     begin
  257.         GetPort(theWind);
  258.         ignore := EWindowClose(theWind);
  259.         CheckFront;
  260.     end;
  261.  
  262.     procedure MakeWind (bindToFile: Boolean);
  263.         var
  264.             r: Rect;
  265.             offset: integer;
  266.             ignore: WindowPtr;
  267.     begin
  268.         if FrontWindow = nil then
  269.             windCount := 0;
  270.         SetRect(r, 0, 0, hSize, vSize);
  271.         windCount := windCount + 1;
  272.         offset := 50 + (25 * (windCount mod 4));
  273.         OffsetRect(r, offset, offset);
  274.         ignore := NewEWindow(r, '', true, WindowPtr(-1), true, longint(0), bindToFile);
  275.     end;
  276.  
  277.  
  278.     procedure AEOpenDocument (spec: FSSpec);
  279.         var
  280.             r: Rect;
  281.             offset: integer;
  282.             ignore: WindowPtr;
  283.     begin
  284.         if FrontWindow = nil then
  285.             windCount := 0;
  286.         SetRect(r, 0, 0, hSize, vSize);
  287.         windCount := windCount + 1;
  288.         offset := 50 + (25 * (windCount mod 4));
  289.         OffsetRect(r, offset, offset);
  290.         ignore := FSpNewEWindow(r, true, WindowPtr(-1), true, 0, spec);
  291.     end;
  292.  
  293.  
  294. {    File menu handler}
  295.  
  296.  
  297.     procedure DoFileMenu (item: integer);
  298.  
  299.         var
  300.             theWind: WindowPtr;
  301.             mypeek: WindowPeek;
  302.             ignore: Boolean;
  303.     begin
  304.         theWind := FrontWindow;
  305.         case item of
  306.             new: 
  307.                 MakeWind(false);
  308.             open: 
  309.                 MakeWind(true);
  310.             close: 
  311.                 if ISEWindow(theWind) then
  312.                     ignore := EWindowClose(theWind)
  313.                 else
  314.                     begin {Non-editing window (Conside, messages…) - just hide it!}
  315.                         HideWindow(theWind);
  316. {mypeek := WindowPeek(theWind);}
  317. {CloseDeskAcc(mypeek^.windowKind);    { DA in front }
  318.                     end;
  319.             save: 
  320.                 ignore := EWindowSave(theWind);
  321.             saveas: 
  322.                 ignore := EWindowSaveAs(theWind);
  323.             revert: 
  324.                 ignore := EWindowRevert(theWind);
  325.             quit: 
  326.                 if ClobberEWindows = true then
  327.                     SkelWhoa;
  328.             otherwise
  329.         end;
  330.         SetNonTextMenus;
  331.     end;
  332.  
  333. {    Handle Font menu items}
  334.  
  335.  
  336.     procedure DoFontMenu (item: integer);
  337.  
  338.         var
  339.             font: integer;
  340.             te: TEHandle;
  341.             theWind: WindowPtr;
  342.             theFontName: Str255;
  343.  
  344.     begin
  345.         theWind := FrontWindow;
  346.         te := GetEWindowTE(theWind);
  347.         if te <> nil then                { not an edit window }
  348.             begin
  349.                 GetMenuItemText(fontMenu, item, theFontName);
  350.                 GetFNum(theFontName, font);
  351.                 SetEWindowSTyle(theWind, font, te^^.txSize, te^^.crOnly, te^^.just);
  352.                 SetTExtMenus(false);
  353.             end;
  354.     end;
  355.  
  356. {    Handle Size menu items }
  357.  
  358.     procedure DoSizeMenu (item: integer);
  359.  
  360.         var
  361.             te: TEHandle;
  362.             theWind: WindowPtr;
  363.     begin
  364.         theWind := FrontWindow;
  365.         te := GetEWindowTE(theWind);
  366.         if te <> nil then
  367.             begin
  368.                 SetEWindowStyle(theWind, te^^.txFont, sizes[item - 1], te^^.crOnly, te^^.just);
  369.                 SetTextMenus(false);
  370.             end;
  371.     end;
  372.  
  373. {    Handle Format menu items }
  374.  
  375.     procedure DoFormatMenu (item: integer);
  376.  
  377.         var
  378.             font, size, just, wrap: integer;
  379.             te: TEHandle;
  380.             theWind: WindowPtr;
  381.  
  382.     begin
  383.         theWind := FrontWindow;
  384.         te := GetEWindowTE(theWind);
  385.         if te <> nil then                {  an edit window }
  386.             begin
  387.                 font := te^^.txFont;
  388.                 size := te^^.txSize;
  389.                 just := te^^.just;
  390.                 wrap := te^^.crOnly;
  391.                 case item of
  392.                     wordWrap: 
  393.                         wrap := 0;
  394.                     noWrap: 
  395.                         wrap := -1;
  396.                     leftJust: 
  397.                         just := teJustLeft;
  398.                     centerJust: 
  399.                         just := teJustCenter;
  400.                     rightJust: 
  401.                         just := teJustRight;
  402.                     otherwise
  403.                 end;
  404.                 SetEWindowStyle(theWind, font, size, wrap, just);
  405.                 SetTextMenus(false);
  406.             end;
  407.     end;
  408.  
  409.  
  410.  
  411.  
  412.     procedure InitEditWindow;
  413.     begin
  414.         lastFront := nil;
  415.         sizes[0] := 9;
  416.         sizes[1] := 10;
  417.         sizes[2] := 12;
  418.         sizes[3] := 14;
  419.         sizes[4] := 18;
  420.         sizes[5] := 20;
  421.         sizes[6] := 24;
  422.         sizes[7] := 48;
  423.  
  424.         fileMenu := NewMenu(1000, 'File');
  425.         AppendMenu(fileMenu, 'New/N;Open…/O;Close/W;(-;Save/S;Save As…');
  426.         AppendMenu(fileMenu, 'Save a Copy As…;Revert/R;(-;Quit/Q');
  427.         dummy := SkelMenu(fileMenu, @DoFileMenu, nil, false);
  428.  
  429.         editMenu := NewMenu(1001, 'Edit');
  430.         AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
  431.         dummy := SkelMenu(editMenu, @EWindowEditOp, nil, false);
  432.  
  433.         fontMenu := NewMenu(1002, 'Font');
  434.         DisAbleItem(fontMenu, 0);
  435.         AppendResMenu(fontMenu, 'FONT');        {AddResMenu}
  436.         dummy := SkelMenu(fontMenu, @DoFontMenu, nil, false);
  437.         sizeMenu := NewMenu(1003, 'Size');
  438.         DisableItem(sizeMenu, 0);
  439.         AppendMenu(sizemenu, '9 Point;10 Point;12 Point;14 Point');
  440.         AppendMenu(sizeMenu, '18 Point;20 Point;24 Point;48 Point');
  441.         dummy := SkelMenu(sizeMenu, @DoSizeMenu, nil, false);
  442.  
  443. {formatMenu := NewMenu(1004, 'Format');}
  444. {DisableItem(formatMenu, 0);}
  445. {AppendMenu(formatMenu, 'Word Wrap;No Word Wrap;(-;Left;Center;Right');}
  446. {dummy := Skelmenu(formatMenu, @DoFormatMenu, nil, true);}
  447.  
  448.         SetNonTextMenus;
  449.         SetTextMenus(true);
  450.  
  451. {    Do TransEdit-specific setup:  set creator for any files created,}
  452. {    set default text style and event notification procedures for}
  453. {    new windows.}
  454.  
  455.         SetEWindowCreator('p4’M');
  456.         SetEWindowStyle(nil, monaco, 9, 0, teJustLeft);
  457.         SetEWindowProcs(nil, @Key, @Activate, @myClose);
  458.  
  459. {    Process events until user quits,}
  460. {    then clean up and exit}
  461.  
  462.         SkelBackground(@CheckFront);
  463.  
  464. {Set Apple Event handler callbacks!}
  465.         ESetAEProcs(@AEOpenDocument, nil);
  466.  
  467.     end;
  468.  
  469. end.